<?php
/**
 * jishupu-cms
 * @Author:技术铺
 * @Date:2021/11/22
 */

namespace App\Services;

/**
 * 路由数据服务
 *
 * Class RouteService
 * @package App\Services
 */
class RouteService
{
    /**
     * 获取所有路由
     * @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable|mixed
     */
    public static function getAllList()
    {
        $result = CacheService::getCache('routeServiceCache');
        if ($result == null) {
            $result = collect(app()->routes->getRoutes())->map(function ($item) {
                $name = $item->action['as'] ?? '';
                $uri = $item->uri ?? '';
                $request_methods = $item->methods ? join('|', $item->methods) : '';
                $controller = "";
                $controller_method = "";
                if (isset($item->action['controller']) && preg_match('/@/', $item->action['controller'])) {
                    $controller_method = $item->action['controller']; // 含控制器方法
                    list($controller, $methods) = explode('@', $controller_method);
                }
                return ['name' => $name, 'uri' => $uri, 'methods' => $request_methods, 'controller' => $controller, 'controller_method' => $controller_method];
            });
            CacheService::setCache('routeServiceCache', $result);
        }
        return $result;
    }

    /**
     * 获取所有用于权限设置的路由列表
     * @param array $disabled_options // 标记禁止选择清单
     * @return array|\Illuminate\Support\Collection
     */
    public static function getPermissionRouteList($disabled_options = [])
    {
        $routeList = collect(self::getAllList())->filter(function ($item) {
            return preg_match('/^admin\..+/i', $item['name']);
        })->values()->all();
        if ($disabled_options) {
            $routeList = collect($routeList)->map(function ($item) use ($disabled_options) {
                if (in_array($item['name'], $disabled_options)) {
                    $item['disabled'] = true;
                }
                return $item;
            });
        }
        return $routeList;
    }

    /**
     * 获取所有权限控制器列表
     * @param array $disabled_options // 标记禁止选择清单
     * @return array|\Illuminate\Support\Collection
     */
    public static function getPermissionRouteControllerList($disabled_options = [])
    {
        return collect(self::getPermissionRouteList())->map(function ($item) use ($disabled_options) {
            $disabled = in_array($item['controller'], $disabled_options);
            return ['controller' => $item['controller'], 'disabled' => $disabled];
        })->unique('controller')->values()->all();
    }

    /**
     * 根据路由别名获取路由信息
     * @param string $name 路由别名
     * @return array|mixed
     */
    public static function getRouteByName($name = "")
    {
        if (!$name) return [];
        $result = collect(self::getAllList())->first(function ($item) use ($name) {
            return $item['name'] == $name;
        });
        return $result ?: [];
    }

    /**
     * 通过路由别名获取路由对应的控制器
     * @param string $name
     * @return mixed|string
     */
    public static function getControllerByAsName($name = "")
    {
        $getRouteByName = self::getRouteByName($name);
        return $getRouteByName ? $getRouteByName['controller'] : "";
    }


    /**
     * 根据控制器获取路由信息
     * @param string $controller 控制器地址
     * @return array|mixed
     */
    public static function getRouteByController($controller = "")
    {
        if (!$controller) return [];
        $result = collect(self::getAllList())->first(function ($item) use ($controller) {
            return $item['controller'] == $controller;
        });
        return $result ?: [];
    }


}
